<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 *
 */
class Problem_Model {

    public $token;
    private $path;
    private $properties;
    private $configurations;
    public $loaded = FALSE;

    public function  __construct($token) {
        $this->token = $token;
        $repo_path = Kohana::config('evaluator.problem_repository_path')."/";
        $this->path = $repo_path.$this->token."/";
        $this->view_path = $this->path."/view/";
        $this->view_files_path = $this->path."/view/files/";
        $this->description_path = $this->view_path . "/description.html";
        $problempath = $this->path."problem.json";
        if (file_exists($problempath))
        {
            $this->properties = json::readfile($problempath);
            $this->configurations = json::readfile($this->path."evaluator/config.json");
            $this->loaded = TRUE;
        }
    }

    public function get_properties($name)
    {
        return $this->properties;
    }

    public function get_property($name)
    {
        return (isset($this->properties[$name]) ? $this->properties[$name] : '');
    }

    /**
     *
     * @return string
     */
    public static function get_problem_repository_path()
    {
        return Kohana::config('evaluator.problem_repository_path')."/";
    }

    public function get_directory_path()
    {
        return self::get_problem_repository_path().$this->token."/";
    }

    public function get_evaluator_directory_path()
    {
        return $this->get_directory_path()."evaluator/";
    }

    public function get_evaluator_file($filename)
    {
        $filename = $this->get_evaluator_path().'files/'.$filename;
        if (file_exists($filename))
        {
            return $filename;
        }
        else
        {
            return NULL;
        }
    }

    public function get_evaluator_script()
    {
        return $this->get_evaluator_directory_path()."evaluator.php";
    }

    public function get_view_directory_path()
    {
        return $this->get_directory_path()."view/";
    }

    public function get_description_path()
    {

    }

    public function get_configuration_file_path()
    {

    }

    private function check_dir($path)
    {

    }

    public function get_configs()
    {

    }

    /**
     *
     * @param string $name
     */
    public function get_config($name)
    {

    }

    public static function get_list()
    {
        $path = Kohana::config('evaluator.problem_repository_path');
        $problems = array();
        $dh = opendir($path);
        while (($file = readdir($dh)) !== false) {
            if ($file == '.' || $file == '..')  continue;
            $absolutepath = $path."/".$file;
            if (filetype($absolutepath) == 'dir')
            {
                $jsonpath = $absolutepath.'/problem.json';
                if (file_exists($jsonpath))
                {
                    $problem = new Problem_Model($file);
                    $problems[] = $problem;
                }
            }
        }
        closedir($dh);
        return $problems;
    }
}